package com.hc.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.hc.bean.BaseStationBean;
import com.hc.util.CommonData;


public class BaseStationFileService {

	public static void  readBaseStationFile(){
		
	File file = new File("./file/baseStationInfo.property");
		
		FileReader fileReader = null;

			try {
				fileReader = new  FileReader(file);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}
			BufferedReader reader = new BufferedReader(fileReader);
			String linStr = "";
			BaseStationBean baseStationBean = null;
			try {
				while((linStr=reader.readLine())!=null){
					
				String[] results= 	linStr.split("=");
					
					if("id".equals(results[0])){
						baseStationBean = new BaseStationBean();
						baseStationBean.setId(Integer.parseInt(results[1]));
						
					}else if("name".equals(results[0])){
						
						baseStationBean.setName(results[1]);
						
					}else if("x".equals(results[0])){
						
						baseStationBean.setX(Integer.parseInt(results[1]));
						baseStationBean.setStartX(Integer.parseInt(results[1]));
					}else if("y".equals(results[0])){
						
						baseStationBean.setY(Integer.parseInt(results[1]));
						baseStationBean.setStartY(Integer.parseInt(results[1]));
						
						CommonData.baseStationBeans.put(baseStationBean.getId(), baseStationBean);
					}
				}
				
				reader.close();
				fileReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		
			
		
			
	}
	public static void saveBaseStationBeans()
	{
		
		StringBuilder sb = new StringBuilder();
		
		
		
	for(Object ob:CommonData.baseStationBeans.keySet()){
			
			BaseStationBean bean = CommonData.baseStationBeans.get(ob);
			sb.append("id="+bean.getId()+"\r\n");
			sb.append("name="+bean.getName()+"\r\n");
			sb.append("x="+bean.getStartX()+"\r\n");
			sb.append("y="+bean.getStartY()+"\r\n");
			
		}
		
		
		File file = new File("./file/baseStationInfo.property");
		
		
		try {
			FileWriter w = new FileWriter(file);
			w.write(sb.toString());
			w.flush();
			w.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
	}
	
	public static void main(String[] args) {
		BaseStationFileService.readBaseStationFile();
		
	StringBuilder sb = new StringBuilder();
		
		
		
		for(Object ob:CommonData.baseStationBeans.keySet()){
			
			BaseStationBean bean = CommonData.baseStationBeans.get(ob);
			
			sb.append("id="+bean.getId()+"\r\n");
			sb.append("name="+bean.getName()+"\r\n");
			sb.append("x="+bean.getX()+"\r\n");
			sb.append("y="+bean.getY()+"\r\n");
			
		}
		
		System.out.println(sb.toString());
		
		BaseStationFileService.saveBaseStationBeans();
		
	}
	
}
